package org.kust.bbs.user.controller;


import org.kust.bbs.common.utils.RandomUtils;
import org.kust.bbs.common.web.JsonResult;
import org.kust.bbs.user.service.MsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/msm")
public class MsmController {
    @Autowired
    private MsmService msmService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 发送注册验证码
     *
     * @param phone
     * @return
     */
    @GetMapping("/send/register")
    public JsonResult<String> sendRegisterCode(String phone) {
        String code = redisTemplate.opsForValue().get(phone);
        if (StringUtils.hasText(code)) {
            return JsonResult.fail("验证码已发送,且在发送完毕两分钟之内有效");
        }
        code = RandomUtils.getSixBitRandom();
        boolean flag = msmService.sendRegister(phone, code);
        if (flag) {
            redisTemplate.opsForValue().set(phone, code, 2, TimeUnit.MINUTES);
            return JsonResult.ok("验证码发送成功");
        }
        return JsonResult.fail("验证码发送失败");
    }

    /**
     * 发送
     *
     * @param phone
     * @return
     */
    @GetMapping("/send/login")
    public JsonResult<String> sendLoginCode(String phone) {
        String code = redisTemplate.opsForValue().get(phone);
        if (StringUtils.hasText(code)) {
            return JsonResult.fail("验证码已发送,且在发送完毕两分钟之内有效");
        }
        code = RandomUtils.getSixBitRandom();
        boolean flag = msmService.sendRegister(phone, code);
        if (flag) {
            redisTemplate.opsForValue().set(phone, code, 2, TimeUnit.MINUTES);
            return JsonResult.ok("验证码发送成功");
        }
        return JsonResult.fail("验证码发送失败");
    }

    /**
     * 发送修改密码验证码
     *
     * @param phone
     * @return
     */
    @GetMapping("/send/update")
    public JsonResult<String> sendUpdateCode(String phone) {
        String code = redisTemplate.opsForValue().get(phone);
        if (StringUtils.hasText(code)) {
            return JsonResult.fail("验证码已发送,且在发送完毕两分钟之内有效");
        }
        code = RandomUtils.getSixBitRandom();
        boolean flag = msmService.sendRegister(phone, code);
        if (flag) {
            redisTemplate.opsForValue().set(phone, code, 2, TimeUnit.MINUTES);
            return JsonResult.ok("验证码发送成功");
        }
        return JsonResult.fail("验证码发送失败");
    }

    /**
     * 发送找回密码验证码
     *
     * @param phone
     * @return
     */
    @GetMapping("/send/retrieve")
    public JsonResult<String> sendRetrieveCode(String phone) {
        String code = redisTemplate.opsForValue().get(phone);
        if (StringUtils.hasText(code)) {
            return JsonResult.fail("验证码已发送,且在发送完毕两分钟之内有效");
        }
        code = RandomUtils.getSixBitRandom();
        boolean flag = msmService.sendRegister(phone, code);
        if (flag) {
            redisTemplate.opsForValue().set(phone, code, 2, TimeUnit.MINUTES);
            return JsonResult.ok("验证码发送成功");
        }
        return JsonResult.fail("验证码发送失败");
    }
}